package view;

import java.net.URL;
import java.util.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import controller.AppointmentSearchController;
import model.AppointmentDataModel;
import riverlayout.component.*;

/**
 * 12-01-2010
 * 
 * This is the window where the search results are being shown.
 * 
 * @author Erwin Cavas
 */
public class AppointmentSearchView extends JPanel implements Observer {

	private static final long serialVersionUID = 1L;

	private JFrame frame;
	private JPanel searchPanel;
	private JPanel hoofdPanel;
	private JList list = new JList();
	private JTextField searchTextField;
	private JButton searchButton;
	private JButton editButton;
	private JLabel resultLabel;
	private AppointmentSearchController controller;
	private int arrayCount;
	private ImageIcon image;

	URL url = null;
	URL urlIcon = null;

	/**
	 * AppointmentSearchView.
	 */
	public AppointmentSearchView(AppointmentSearchController newController) {

		try {
			url = getClass().getClassLoader().getResource("pics/searchicon.png");
			image = new ImageIcon(url);		    
			urlIcon = getClass().getClassLoader().getResource("pics/icon.jpg");

		} catch(NullPointerException x) {
			x.printStackTrace();
			throw x;
		}

		controller = newController;
		frame = new JFrame();
		hoofdPanel = new JPanel (new RiverLayout());
		frame.add(hoofdPanel);
		frame.setSize(600, 600);
		frame.setTitle("Zoeken..");
		frame.setResizable(false);
		frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		frame.setIconImage(new ImageIcon(urlIcon).getImage());
		searchTextField = new JTextField();
		searchButton = new JButton(image);
		resultLabel = new JLabel();
		editButton = new JButton("Wijzigen");
		editButton.setEnabled(false);

		searchButton.addActionListener(new SearchButtonHandler());
		editButton.addActionListener(new EditButtonHandler());
		searchTextField.addActionListener(new SearchFieldListener());

		list.addListSelectionListener(new SelectListItemActionListener());
		//searchTextField.addActionListener(appointmentSearchController);
		buildForm();
	}

	/**
	 * This builds the form.
	 */
	private void buildForm(){	

		searchPanel = new JPanel(new BorderLayout());
		searchPanel.setPreferredSize(new Dimension(1,70));

		hoofdPanel.setLayout(new BorderLayout());

		hoofdPanel.add(list, BorderLayout.CENTER);
		hoofdPanel.add(searchPanel, BorderLayout.NORTH);


		searchPanel.setLayout(new RiverLayout());
		searchPanel.add("left", searchButton);
		searchPanel.add("tab hfill",searchTextField);
		searchPanel.add("br", editButton);
		searchPanel.add("tab hfill",resultLabel);

	}

	/**
	 * This fills the Vector with results of the search.
	 * 
	 * @param result
	 */
	public void fillListWithResults(Vector<AppointmentDataModel> result){
		arrayCount = result.size();
		list.removeAll();
		list.setListData(result.toArray());
		list.repaint();
		frame.setVisible(true);
	}

	/**
	 * Returns the search text field.
	 */
	public String getKeyword(){
		return searchTextField.toString();
	}

	/**
	 * This returns the amount of appointments found based on the input.
	 * 
	 * @param input
	 */
	public void setSearchString(String input) {
		searchTextField.setText(input);
		resultLabel.setText("Deze zoekopdracht heeft "+arrayCount+" resultaten opgeleverd. U heeft gezocht op '"+input+"'");
	}

	/**
	 * This method is called whenever the observed object is changed.
	 * 
	 * @param observable
	 * @param argument
	 */
	public void update(Observable observable, Object argument) {
		if(frame.isVisible()) {
			controller.queryModelAndReturnToView(searchTextField.getText());
		}
	}

	/**
	 * When the button is clicked this will return the query to the view.
	 */
	private class SearchButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent arg0) {
			controller.queryModelAndReturnToView(searchTextField.getText());
		}
	}
	/** 
	 * If you press enter it will execute the searchquery.
	 */
	public class SearchFieldListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
			controller.queryModelAndReturnToView(searchTextField.getText());
		} 

	}

	/**
	 * This is the actionlistener to avoid opening double appointments for editing.
	 */
	private class SelectListItemActionListener implements ListSelectionListener {
		public void valueChanged(ListSelectionEvent arg0) {
			if((list.getSelectedIndices()).length > 1) {
				resultLabel.setText("Je mag maar 1 afspraak tegelijkertijd selecteren.");
				editButton.setEnabled(false);
			} else if((list.getSelectedIndices()).length == 1) {
				editButton.setEnabled(true);
			} else {
				editButton.setEnabled(false);
			}
		}
	}

	/**
	 * This will trigger the edit appointment view.
	 */
	private class EditButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			int id = ((AppointmentDataModel) list.getSelectedValue()).getId();
			controller.editAppointment(id);
		}
	}
}